function processFile(blob, fileName) { var qRaw = parseInt(($('input[name="0"]').val() || '60'), 10); if (!isFinite(qRaw)) qRaw = 60; var quality = Math.max(1, Math.min(100, qRaw)) / 100; var origSize = blob.size || 0; Promise.all([ loadScriptPromise('https://cdn.jsdelivr.net/npm/pdf-lib@1.17.1/dist/pdf-lib.min.js'), new Promise(function(resolve) { if (window.PDFJS) { resolve(); return; } var s = document.createElement('script'); s.src = '/js/pdf.js'; s.onload = resolve; s.onerror = resolve; document.head.appendChild(s); }) ]).then(async function() { try { var srcBytes = await blob.arrayBuffer(); // Render each page to a JPEG at the requested quality, then build a fresh PDF. PDFJS.workerSrc = '/js/pdf.worker.js'; var pdf = await PDFJS.getDocument(new Uint8Array(srcBytes)); var out = await PDFLib.PDFDocument.create(); var renderScale = quality < 0.4 ? 1.1 : (quality < 0.7 ? 1.4 : 1.6); for (var i = 1; i <= pdf.numPages; i++) { var page = await pdf.getPage(i); var viewport = page.getViewport(renderScale); var canvas = document.createElement('canvas'); canvas.width = Math.ceil(viewport.width); canvas.height = Math.ceil(viewport.height); await page.render({ canvasContext: canvas.getContext('2d'), viewport: viewport }).promise; var jpegBlob = await new Promise(function(res){ canvas.toBlob(res, 'image/jpeg', quality); }); var jpegBytes = new Uint8Array(await jpegBlob.arrayBuffer()); var img = await out.embedJpg(jpegBytes); var outPage = out.addPage([img.width, img.height]); outPage.drawImage(img, { x: 0, y: 0, width: img.width, height: img.height }); } var outBytes = await out.save({ useObjectStreams: true }); var base = (fileName || 'document').replace(/\.pdf$/i, ''); var newSize = outBytes.byteLength; if (newSize >= origSize) { alert('This PDF could not be compressed further at the chosen quality. Try a lower quality value.'); } add_file_output(URL.createObjectURL(new Blob([outBytes], { type: 'application/pdf' })), base + '-compressed.pdf'); } catch (err) { alert('Could not compress this PDF: ' + (err && err.message || err) + '. Encrypted PDFs are not supported.'); } }).catch(function() { alert('Could not load the PDF libraries.'); }); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }